﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ResultsManagementSystem
{
    public partial class TeacherInterface : Form
    {
        private int teacherId;

        public TeacherInterface(int teacherId)
        {
            InitializeComponent();

            this.teacherId = teacherId;
        }

        private void 录入成绩ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void putResult_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void putSubjectInformation_Click(object sender, EventArgs e)
        {
            Subject subject = new Subject(this.subjectNameBox.Text,this.teacherId);

            SubjectDao subjectDao = new SubjectDao(subject);

            if (subjectDao.insertSubject())
            {
                MessageBox.Show("插入成功！");
            }
            else
            {
                MessageBox.Show("表已存在或者其他原因导致插入失败！");
            }
        }

        private void putScore_Click(object sender, EventArgs e)
        {
            SubjectDao subjectDao = new SubjectDao();

            Subject subject = subjectDao.querrySubjectByName(this.subjectNameBoxPutScore.Text);

            if(subject == null)
            {
                MessageBox.Show("课程不存在!");
            }
            else
            {
                //插入学生成绩
                ScoreDao scoreDao = new ScoreDao();

                if (scoreDao.insertScore(this.subjectNameBoxPutScore.Text, this.numberBox.Text, Convert.ToInt32(this.scoreBox.Text)))
                {               
                    //插入学生课程表
                    StudentDao studentDao = new StudentDao();

                    Student student = studentDao.querryStudentByNumber(this.numberBox.Text);

                    if (student == null)
                    {
                        MessageBox.Show("学生不存在！");

                        return;
                    }

                    StudentSubjectDao studentSubjectDao = new StudentSubjectDao();

                    if (studentSubjectDao.insertStudentSubject(student.getId(), subject.getId()))
                    {
                        MessageBox.Show("插入成功！");
                    }
                    else
                    {
                        MessageBox.Show("插入学生课程表时发生错误！");
                    }
                }
                else
                {
                    MessageBox.Show("学生成绩已存在或其他原因导致插入失败！");
                }
            }
        }

        private void queryButton_Click(object sender, EventArgs e)
        {
            List<Score> scoreList = new List<Score>();

            ScoreDao scoreDao = new ScoreDao();

            Score score = new Score();

            //查询，并显示成绩
            scoreList = scoreDao.querryScoreListBySubjectName(this.subjectNameQueryBox.Text);

            if(scoreList == null)
            {
                MessageBox.Show("课程不存在!");

                return;
            }

            if(scoreList.Count == 0)
            {
                MessageBox.Show("课程不存在!");
            }
            else
            {
                if (this.subjectNumberQueryBox.Text == null || this.subjectNumberQueryBox.Text == "")
                {
                    //查询整体成绩
                    //清空ListBox
                    this.scoreListBox.Items.Clear();

                    //重新填入数据
                    for (int i = 0; i < scoreList.Count; i++)
                    {
                        score = scoreList.ElementAt(i);

                        scoreListBox.Items.Add(score.getId().ToString() + " " + score.getNumber() + " " + score.getScore().ToString());
                    }
                }
                else
                {
                    //查询单个成绩
                    score = scoreDao.querryScoreByNumberAndSubjectName(this.subjectNameQueryBox.Text, this.subjectNumberQueryBox.Text);

                    //不存在成绩
                    if (score == null)
                    {
                        MessageBox.Show("学生成绩不存在！");
                    }
                    else
                    {
                        //清空ListBox
                        this.scoreListBox.Items.Clear();

                        //重新填入数据
                        scoreListBox.Items.Add(score.getId().ToString() + " " + score.getNumber() + " " + score.getScore().ToString());
                    }
                }

            }
        }

        private void queryResult_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}
